import Vue from 'vue'
import VueRouter from 'vue-router'
import el from "element-ui/src/locale/lang/el";


// 路由懒加载
const Login = () => import("../views/login/Login")
const Home = () => import("../views/home/Home")
const Welcome = () => import("../views/home/children/Welcome")
const User = () => import("../views/user/User")
const Roles = () => import("../views/power/Roles")
const Rights = () => import("../views/power/Rights")
const Cate = () => import("../views/goods/Cate")
const Params = () => import("../views/goods/Params")
const List = () => import("../views/goods/List")
const Add = () => import("../views/goods/Add")
const Order = () => import('../views/order/Order')
const Report = () => import('../views/report/Report')
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                name: 'welcome',
                component: Welcome
            },
            {
                path: '/users',
                name: 'users',
                component: User
            }, {
                path: '/roles',
                name: 'roles',
                component: Roles
            }, {
                path: '/rights',
                name: 'rights',
                component: Rights
            }, {
                path: '/categories',
                name: 'categories',
                component: Cate
            }, {
                path: '/params',
                name: 'params',
                component: Params
            }, {
                path: '/goods',
                name: 'goods',
                component: List
            }, {
                path: '/goods/add',
                component: Add
            }, {
                path: '/orders',
                component: Order
            }, {
                path: '/reports',
                component: Report
            }
        ]
    }

]

const router = new VueRouter({
    routes
})
// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to, from, next) => {
    //访问登录页面，放行
    if (to.path == '/login') {
        return next()
    } else {
        // 获取token
        const tokenStr = window.sessionStorage.getItem('token')
        // token为空
        if (!tokenStr) {
            next('/login')
        } else {
            next()
        }
    }
})

export default router
